package com.example.qxfw.qxybzz.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.qxybzz.entity.constantfcst;
import com.example.qxfw.qxybzz.service.ServiceMaterialsService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.datasource.DataSourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/***
 * 服务产品材料制作
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/serviceMater")
public class ServiceMaterialsController {

    @Autowired
    private ServiceMaterialsService serviceMaterialsService;

    /***
     * 积雪材料制作，上传dat文件，制作伊犁河谷的内容
     * @param multipartFile
     * @return
     */
    @RequestMapping("/uploadDat")
    public SysResult uploadDat(@RequestParam("file") MultipartFile multipartFile) {
        if (multipartFile.getOriginalFilename().endsWith(".dat") && !multipartFile.isEmpty()) {
            File file = null;
            try {
                file = File.createTempFile(multipartFile.getOriginalFilename().replace(".dat", ""), ".dat");
                InputStream inputStream = multipartFile.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                // 将 MultipartFile 内容写入到临时文件
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }

                ArrayList<Object> list = serviceMaterialsService.AnalyzeDat(file, multipartFile.getOriginalFilename());
                return SysResult.success(list);

            } catch (DataSourceException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                // 设置JvM退出时删除临时文件
                file.deleteOnExit();
            }
        }
        return SysResult.fail("上传的文件为空或格式不正确！");
    }

    /***
     * 更新修改的数值
     * @param cconstantfcst
     * @return
     */
    @RequestMapping("/updateConstantfcst")
    public SysResult updateConstantfcst(constantfcst cconstantfcst) {
        int flag = serviceMaterialsService.updateConstantfcst(cconstantfcst);
        if (flag > 0) {
            return SysResult.success("更新成功!");
        } else {
            return SysResult.fail("更新失败!");
        }
    }

    /***
     * 获取产品最新期数
     * @param producttype "积雪遥感监测专报"
     * @return
     */
    @ApiOperation("获取产品最新期数")
    @GetMapping("getMaxId")
    public SysResult getMaxId(String producttype) {
        Integer maxId = serviceMaterialsService.getMaxId(producttype);
        return SysResult.success(maxId);
    }

    /***
     * 制作积雪（流域名称、时间yyyy-MM-dd、期号、伊犁柱状图、伊犁等值图、流域柱状图、流域等值图）
     * @param lyname 喀什河、特克斯河
     * @param datetime
     * @param qishu
     * @param images
     * @return
     */
    @RequestMapping("/makeConstantDoc")
    public Object makeConstantDoc(String lyname, String datetime, String qishu, @RequestParam("images") MultipartFile[] images) {
        return serviceMaterialsService.makeConstantDoc(lyname, datetime, qishu , images);
    }
}
